#!/bin/bash
########################################################################
# script name:  check_tivoli.sh
# Used for:
# Author:       Martin Dumont
# Contributors:
# Version:      0.1
#
# (date format: date +'%Y/%m/%d')
#
# Date          Version Contributors    Modifications
# ----------    ------- ------------    -------------
# 2011/05/10    0.1     MDUMONT         initial
########################################################################
usage()
{
cat << STOP

usage: $0 [options] [ARG]

This script is used for ....

OPTIONS:
   -a               All hosts
   -h HOST          Host name
   -i               Show only INSTALLED or not
   -r               Show only RUNNING or not

Ex.:
$0 -a
$0 -h saprmt01
$0 -i -h all
$0 -r -h all
STOP
}
#######################################
# Get parameters
#######################################
INSTALLEDOPTION=1
RUNNINGOPTION=1
while getopts airh: OPTION
do
        case $OPTION in
        h)
                HOSTS=$(echo $OPTARG | sed 's/,/ /g' | sed 's/^-*//' | cut -d. -f1 | tr '[A-Z]' '[a-z]')
                ;;
        a)
                HOSTS=all
                ;;
        i)
                INSTALLEDOPTION=1
                RUNNINGOPTION=0
                ;;
        r)
                RUNNINGOPTION=1
                INSTALLEDOPTION=0
                ;;
        \?)
                usage
                exit 1
                ;;
        *)
                usage
                exit 1
                ;;
        esac
done
if [ $OPTIND = 1 ]; then usage; exit 1; fi
#######################################
# Variables and environnement
#######################################
CINFO=/opt/IBM/tivoli/bin/cinfo
BAENV=$(dirname `which $0`)/loadenv.sh
if [ -f $BAENV ]; then . $BAENV; else echo "No env library ($BAENV)"; exit 1; fi
#######################################
# MAIN
#######################################
NEWHOSTLIST=$(checkhost $HOSTS | grep -v Verifying)
for HOSTNAME in $NEWHOSTLIST
do
	INSTALL=$(sudo -u saunix ssh -x -q saunix@$HOSTNAME "sudo sh -c \"if [ -x $CINFO ]; then echo ok; else echo NOT installed; fi\"")
	if [[ $INSTALLEDOPTION = 1 ]]
	then
		echo -e "Verifying Tivoli installation on $HOSTNAME ...\c"
		echo $INSTALL
	fi
	if [[ $RUNNINGOPTION = 1 ]]
	then
		if [[ "$INSTALL" = "ok" ]]
		then
			echo -e "Verifying Tivoli process on $HOSTNAME ...\c"
			RUN=$(sudo -u saunix ssh -x -q saunix@$HOSTNAME "sudo $CINFO -r" | grep -q 'no known processes'; RC=$?; if [[ $RC != 0 ]]; then echo ok; else echo NOT running; fi)
			echo $RUN
		else
			echo -e "Tivoli is not INSTALLED on $HOSTNAME, skipping verification check"
		fi
	fi
done
exit 0
